﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.Locomotive.Plane
{
    public partial class Locomotive_Train_Gant_Map : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID = "-1";
        public string _LoadName = "-1";
        public string _sTime = "";
        public string _eTime = "";
        public Locomotive_Train_Gant_Map()
        {
            InitializeComponent();
        }

        private void Locomotive_Plan_Schedule_Month_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        private void Locomotive_Plan_Schedule_Month_Load(object sender, EventArgs e)
        {
            #region//--初始化色块规则--//
            DevExpress.XtraEditors.FormatConditionRuleValue[,] fcr_Main = new FormatConditionRuleValue[32,9];
            for (int x = 1; x < 32; x++)
            {
                for (int y = 1; y < 9; y++)
                {
                    fcr_Main[x, y] = new DevExpress.XtraEditors.FormatConditionRuleValue();
                    fcr_Main[x, y].Appearance.Options.UseBackColor = true;
                    fcr_Main[x, y].Condition = DevExpress.XtraEditors.FormatCondition.Equal;
                    switch(y)
                    {
                        case 1:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.White;
                            fcr_Main[x, y].Value1 = "White";
                            break;
                        case 2:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Gray;
                            fcr_Main[x, y].Value1 = "Gray";
                            break;
                        case 3:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Red;
                            fcr_Main[x, y].Value1 = "Red";
                            break;
                        case 4:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Yellow;
                            fcr_Main[x, y].Value1 = "Yellow";
                            break;
                        case 5:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.LightBlue;
                            fcr_Main[x, y].Value1 = "LightBlue";
                            break;
                        case 6:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Magenta;
                            fcr_Main[x, y].Value1 = "Magenta";
                            break;
                        case 7:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Cyan;
                            fcr_Main[x, y].Value1 = "Cyan";
                            break;
                        case 8:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Green;
                            fcr_Main[x, y].Value1 = "Green";
                            break;
                    }
                }
            }
            DevExpress.XtraGrid.GridFormatRule _gfr = new DevExpress.XtraGrid.GridFormatRule();
            for (int i = 1; i < 32; i++)
            {
                for (int n = 1; n < 9; n++)
                {
                    _gfr = new DevExpress.XtraGrid.GridFormatRule();
                    _gfr.Column = this.bgv_Main.Columns["f_c" + i.ToString().PadLeft(2, '0')];
                    _gfr.ColumnApplyTo = this.bgv_Main.Columns["f_d" + i.ToString().PadLeft(2, '0')];
                    _gfr.Name = "Format_" + i.ToString().PadLeft(2, '0') + "_" + n.ToString();
                    _gfr.Rule = fcr_Main[i, n];
                    bgv_Main.FormatRules.Add(_gfr);
                }
            }
            gc_Main.Refresh();
            #endregion
            _LoadData();
        }

        private void bt_Exp_PDF_Click(object sender, EventArgs e)
        {
            bgv_Main.ExportToPdf(TS_Win_Class._EXP_Direct + _LoadName + "作业甘特图.pdf");
            MessageBox.Show("文件导出为 " + TS_Win_Class._EXP_Direct + _LoadName + "作业甘特图.pdf");
        }

        private void gc_Main_DoubleClick(object sender, EventArgs e)
        {
            Locomotive_Train_Gant_ChangeDay _uc = new Locomotive_Train_Gant_ChangeDay();
            _uc._LoadBMWID = _LoadID;
            _uc._LoadName = _LoadName+ bgv_Main.GetFocusedRowCellValue("f_name").ToString();
            _uc._LoadBMWDID = bgv_Main.GetFocusedRowCellValue("f_bmwdid").ToString();
            _uc._LoadStime = bgv_Main.GetFocusedRowCellValue("f_pstime").ToString();
            _uc._LoadEtime = bgv_Main.GetFocusedRowCellValue("f_petime").ToString();
            if (_uc.ShowDialog()==DialogResult.OK)
            {
                _LoadData();
            }
        }

        private void bt_Exp_Excel_Click(object sender, EventArgs e)
        {
            DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
            bgv_Main.ExportToXlsx(TS_Win_Class._EXP_Direct+ _LoadName + "作业甘特图.xlsx");
            MessageBox.Show("文件导出为 "+TS_Win_Class._EXP_Direct + _LoadName + "作业甘特图.xlsx");
        }

        private void gc_Main_Click(object sender, EventArgs e)
        {

        }
        private void _LoadData()
        {
            string[] _time = TS_Win_Class.GetSQlFieldValue("select CONVERT(varchar(100),f_pitime,23) as f_pitime,datediff(day,f_pitime,f_pltime) as f_day,CONVERT(varchar(100),f_pltime,23) as f_pltime from TS_TR_BU_Work_Main where f_id=" + _LoadID, new string[] { "f_pitime", "f_pltime", "f_day" });
            _sTime = _time[0];
            _eTime = _time[1];
            this.Text = _LoadName + "作业甘特图";
            DateTime _Day = Convert.ToDateTime(_sTime);
            for (int i = 0; i < Convert.ToInt32(_time[2]) + 1; i++)
            {
                foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand _cl in gb_f_x01.Children)
                {
                    if (_cl.Name == "gb_f_d" + (i + 1).ToString().PadLeft(2, '0'))
                    {
                        _cl.Children[0].Caption = TS_Win_Class._GetWeekString(_Day.AddDays(i))[0];
                        _cl.Caption = _Day.AddDays(i).Day.ToString();
                    }
                }
            }
            for (int i = Convert.ToInt32(_time[2]) + 1; i < 32; i++)
            {
                foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand _cl in gb_f_x01.Children)
                {
                    if (_cl.Name == "gb_f_d" + i.ToString().PadLeft(2, '0'))
                    {
                        _cl.Children[0].Visible = false;
                        _cl.Visible = false;
                    }
                }
            }
            gb_f_x01.Caption = _sTime + "  至  " + _eTime;
            string strSQL = "delete TS_TR_TMP_Work_Gant where f_bmwid=" + _LoadID;
            TS_Win_Class.MySqlCom(strSQL);
            strSQL = "select f_id,f_name,f_pstime,f_petime,datediff(day,'" + _sTime + "',f_pstime) as f_sday,datediff(day,'" + _sTime + "',f_petime) as f_eday,(select f_string from TS_S_T_S_ColorList where f_id =(select f_cid from TS_S_T_S_RP_Name_Dictionary where f_id=(select f_ndid from TS_TR_PS_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Day.f_mwdid))) as f_color from TS_TR_BU_Work_Main_Day where f_del=0 and f_bmwid=" + _LoadID + "  order by f_order,f_pstime asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                string _Filed = "f_bmwid,f_bmwdid,f_name,f_pstime,f_petime";
                string _Value = _LoadID + "," + read["f_id"].ToString() + ",'" + read["f_name"].ToString() + "','" + read["f_pstime"].ToString() + "','" + read["f_petime"].ToString() + "'";
                for (int i = (int)read["f_sday"]; i < (int)read["f_eday"] + 1; i++)
                {
                    _Filed += ",f_c" + (i + 1).ToString().PadLeft(2, '0');
                    if (read["f_color"].ToString() == "White")
                    {
                        _Value += ",'Gray'";
                    }
                    else
                    {
                        _Value += ",'" + read["f_color"].ToString() + "'";
                    }
                }
                strSQL = "insert into TS_TR_TMP_Work_Gant(" + _Filed + ") values(" + _Value + ")";
                TS_Win_Class.MySqlCom(strSQL);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            strSQL = "select row_number() over (order by f_id asc) as f_row,*,CONVERT(varchar(100), f_pstime, 23) as f_pstime,CONVERT(varchar(100), f_petime, 23) as f_petime from TS_TR_TMP_Work_Gant where f_bmwid=" + _LoadID;
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, _dt);
            gc_Main.DataSource = _dt;
        }
    }
}